package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.Impl.ProductCatalogueDaoImpl;
import com.ysd.entity.Product;
import com.ysd.entity.ProductCatalogue;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminProductCatalogueServlet extends BaseServlet {
	/**
	 * 获得产品分类
	 *localhost:8080/huaduoyoupin/adminProductCatalogue?reqName=getProductCatalogue
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProductCatalogue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductCatalogueDaoImpl pcDao=new ProductCatalogueDaoImpl();//创建实现类对象
		int page=Integer.parseInt(request.getParameter("page"));
		int size=Integer.parseInt(request.getParameter("size"));
		
		List<ProductCatalogue> pcList=pcDao.getproductCateloguePage(page, size);//分页查询所有产品分类
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式
		PrintWriter out=response.getWriter();//out对象
		//发送ajax请求服务器会返回json字符串格式
		Gson gson=new Gson();//创建Gson对象
		
		String res=gson.toJson(pcList);//将pcList转化为json格式的字符串
		out.print(res);//响应数据
		
		
		
		
		
		out.flush();
		out.close();
	}
	//分页查询所有
	//localhost:8080/huaduoyoupin/adminProductCatalogue?reqName=getAllProductCatalogue
	public void getAllProductCatalogue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductCatalogueDaoImpl pcDao=new ProductCatalogueDaoImpl();//创建实现类对象

		List<ProductCatalogue> pcList=pcDao.getPCList();//查询所有产品分类
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式
		PrintWriter out=response.getWriter();//out对象
		//发送ajax请求服务器会返回json字符串格式
		Gson gson=new Gson();//创建Gson对象
		
		String res=gson.toJson(pcList.size());//将pcList转化为json格式的字符串
		out.print(res);//响应数据
		
		
		
		
		
		out.flush();
		out.close();
	}
    /****
     * 删除产品分类
     * localhost:8080/huaduoyoupin/adminProductCatalogue?reqName=delProductCatalogue
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
	
	public void delProductCatalogue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接受前台传来的参数
		int id=Integer.parseInt(request.getParameter("catalogueId"));
		ProductCatalogueDaoImpl pcDao=new ProductCatalogueDaoImpl();
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式
		PrintWriter out=response.getWriter();//out对象
		
		int n=pcDao.delProductCatalogueId(id);//调用实现类的删除方法，返回受影响行数
		if(n>0) {//删除成功
			out.print(Result.toClient(true,"删除成功",""));
		}else {//删除失败
			out.print(Result.toClient(false,"删除失败",""));
		}
		
		out.flush();
		out.close();
	}
	
	/***
	 * localhost:8080/huaduoyoupin/adminProductCatalogue?reqName=addProductCatalogue
	 * 添加产品系列 
	 */
	public void addProductCatalogue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		ProductCatalogueDaoImpl pcDao=new ProductCatalogueDaoImpl();
		String productCatalogueName=request.getParameter("CatalogueName");
		String productCatalogueImg=request.getParameter("CatalogueImg");
		if(productCatalogueName!=null && !productCatalogueName.equals("")) {
			List<ProductCatalogue> pcList=pcDao.getProductCatalogueByName(productCatalogueName);
			if(pcList.size()==0) {
				ProductCatalogue pc=new ProductCatalogue(0, productCatalogueName, productCatalogueImg);
				
				int n=pcDao.addProductCatalogue(pc);
				if(n>0) {
					out.print(Result.toClient(true, "添加成功",""));
				}else {
					out.print(Result.toClient(false, "添加失败",""));
				}
			}else {
				out.print(Result.toClient(false, "分类名称已存在",""));
			}
		}
		
		
		
		out.flush();
		out.close();
	}
	/***
	   *       根据id查询
	 *  localhost:8080/huaduoyoupin/adminProductCatalogue?reqName=getProductCatalogueById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProductCatalogueById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		ProductCatalogueDaoImpl pcDao=new ProductCatalogueDaoImpl();
		int cateId=Integer.parseInt(request.getParameter("cId"));//接收前台传来的参数并转换为int类型
		
		ProductCatalogue pc=pcDao.getProductCatalogueById(cateId);//调用实现类根据产品分类id查询产品分类
		
		Gson gson=new Gson();
		String res=gson.toJson(pc);//将pc对象转化为json字符串
		out.print(res);//响应数据
		
		out.flush();
		out.close();
	}
	/**
	 *  localhost:8080/huaduoyoupin/adminProductCatalogue?reqName=updateProductCatalogueById
	 * 修改分类名称
	 */
	public void updateProductCatalogueById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		ProductCatalogueDaoImpl pcDao=new ProductCatalogueDaoImpl();
		int productCatalogueId=Integer.parseInt(request.getParameter("productCatalogueId"));
		String productCatalogueName=request.getParameter("productCatalogueName");
		if(productCatalogueName!=null&&!productCatalogueName.equals("")) {
			List<ProductCatalogue> pcList=pcDao.getProductCatalogueByName(productCatalogueName);
			if(pcList.size()==0) {//没有查到
				ProductCatalogue pc=new ProductCatalogue(productCatalogueId, productCatalogueName);
				int n=pcDao.updateProductCatalogueById(pc);
				if(n>0) {
					out.print(Result.toClient(true, "修改成功",""));
				}else {
					out.print(Result.toClient(false, "修改失败",""));
				}
			}else {
				out.print(Result.toClient(false, "分类名已存在",""));
			}
		}

		out.flush();
		out.close();
	}
	//localhost:8080/huaduoyoupin/adminProductCatalogue?reqName=delectProductCatalogueByIds
	//批量删除
	public void delectProductCatalogueByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		ProductCatalogueDaoImpl pcDao=new ProductCatalogueDaoImpl();
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式
		PrintWriter out=response.getWriter();//out对象
		String ids=request.getParameter("ids");
		int n=pcDao.delectProductCatalogueByIds(ids);
	    //调用实现类的删除方法，返回受影响行数
		if(n>0) {//删除成功
			out.print(Result.toClient(true,"删除成功",""));
		}else {//删除失败
			out.print(Result.toClient(false,"删除失败",""));
		}
		
		out.flush();
		out.close();
	}
}
